package com.example.buttontest;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 
       View objButton = findViewById(R.id.button1);
       objButton.setOnClickListener(this);         
    }
    
    public void onClick(View v) {

     Toast.makeText(this, "Нажали кнопку", Toast.LENGTH_SHORT).show();
     TextView textView = (TextView) findViewById(R.id.textView1);
     //textView.setText(R.string.string2);
     double randomvalue = Math.random();
   /*  if (randomvalue <=1) {
    	 textView.setText(R.string.string1);
     } else { 
    	 textView.setText(R.string.string2);
     } */
     
     //Bundle bundle = getIntent().getExtras();
     //String itemname = "n" + bundle.getString("defStrID");
     Context context = getBaseContext(); 
     String text = readRawTextFile(context, getResources().getIdentifier("words", "raw", "com.example.buttontest"));
     textView.setText(text);
    } 

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
     
    public static String readRawTextFile(Context ctx, int resId)
    {
       InputStream inputStream = ctx.getResources().openRawResource(resId);
   
        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);
         String line;
         StringBuilder text = new StringBuilder();
   
         try {
          while (( line = buffreader.readLine()) != null) {
            text.append(line);
            text.append('\n');
           }
        } catch (IOException e) {
          Log.e("read",
              "read data: error!"
                  + e.getMessage(), e);
        }
         return text.toString();
    }
}